<template>
    <Modal v-model="modal.show" width="350" :title="modal.title">
        <Form ref="form" :model="formItem" :rules="ruleValidate" :label-width="100">
            <FormItem label="代码" prop="code1"><Input v-model="formItem.code1" placeholder="" style="width: 200px" />
            </FormItem>
            <FormItem label="名称" prop="name1"><Input v-model="formItem.name1" placeholder="" style="width: 200px" />
            </FormItem>
            <FormItem label="类别代码" prop="code2"><Input v-model="formItem.code2" placeholder="" style="width: 200px" />
            </FormItem>
            <FormItem label="类别名称" prop="name2"><Input v-model="formItem.name2" placeholder="" style="width: 200px" />
            </FormItem>
        </Form>
        <template #footer>
            <div>
                <Button type="text" @click="modal.show = false">取消</Button>
                <Button type="primary" @click="handleOk">确定</Button>
            </div>
        </template>
    </Modal>
</template>
<script setup>
import http from '@/util/http'
import _ from 'lodash'
let { proxy } = getCurrentInstance()
const emit = defineEmits(['initList'])
let modal = reactive({ show: false, title: '' })
const form = ref()

let formItem = reactive({
    group: '辅助目录',
    code1: '',
    name1: '',
    code2: '',
    name2: '',
})

const show = (type, data) => {
    form.value.resetFields()
    modal.show = true
    formItem.spid = ''
    if (type == 1) {
        modal.title = '添加辅助目录'
    } else {
        var clone = _.cloneDeep(data)
        formItem = clone
        modal.title = '编辑辅助目录'
    }
}

const ruleValidate = {
    code1: [{ required: true, message: '请输入', trigger: 'blur' }],
    code2: [{ required: true, message: '请输入', trigger: 'blur' }],
    name1: [{ required: true, message: '请输入', trigger: 'blur' }],
    name2: [{ required: true, message: '请输入', trigger: 'blur' }],
}

const handleOk = () => {
    form.value.validate((valid) => {
        if (valid) {
            http.post('/Basic/PolicyEnum/AddOrUpdate', toRaw(formItem), (response) => {
                if (response.data.flag) {
                    emit('initList')
                    proxy.$Message.success(response.data.msg)
                    modal.show = false
                } else {
                    proxy.$Message.error(response.data.msg)
                }
            })
        } else {
            return false
        }
    })
}

defineExpose({
    show,
})
</script>